package com.oa.srms.apps.actions.goods;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.oa.srms.apps.actions.BasicAction;
import com.oa.srms.apps.business.goods.IGoodCategoryService;
import com.oa.srms.apps.business.goods.IGoodForCategoryService;
import com.oa.srms.apps.business.goods.IGoodInfoService;
import com.oa.srms.apps.business.goods.IGoodReceivedService;
import com.oa.srms.apps.business.pay.IPayInfoService;
import com.oa.srms.apps.business.student.IStudentInfoService;
import com.oa.srms.apps.constants.IPayStates;
import com.oa.srms.apps.constants.IReceiveStates;
import com.oa.srms.apps.constants.IStudentStates;
import com.oa.srms.apps.entity.goods.GoodForCategoryValue;
import com.oa.srms.apps.entity.goods.GoodInfoValue;
import com.oa.srms.apps.entity.goods.GoodReceivedValue;
import com.oa.srms.apps.entity.pay.PayInfoValue;

@SuppressWarnings("serial")
public class GoodReceivedAction extends BasicAction {
	
	@Autowired
	private IGoodReceivedService goodReceivedService;
	@Autowired
	private IGoodForCategoryService goodForCategoryService;
	@Autowired
	private IGoodCategoryService goodCategoryService;
	@Autowired
	private IGoodInfoService goodInfoService;
	@Autowired
	private GoodForCategoryValue goodForCategoryValue;
	@Autowired
	private IStudentInfoService studentInfoService;
	@Autowired
	private IPayInfoService payInfoService;
	
	private List<GoodReceivedValue> goodReceivedList;
	
	private Integer id;
	private Integer stuId;
	private String stuNo;
	private Integer categoryId;
	private String comments;
	private Integer states;
	private String str;
	private String cardId;
	
	private String checkGoodInfoIds;
	private String unCheckGoodInfoIds;
	
	
	public String toShow() throws Exception{
		return "toShowPage";
	}
	
	public String toQuery() throws Exception{
		return "toQueryPage";
	}
	
	public String toReceive() throws Exception{
		return "toReceivePage";
	}
	
	public String addGoodReceived() throws Exception {
		goodReceivedService.addGoodReceived(stuId, categoryId);
		studentInfoService.setStudentStates(stuId, IStudentStates.CHECK);
		List<PayInfoValue> list = payInfoService.queryPayInfo(cardId);
		for(PayInfoValue payInfo : list){
			if(payInfo.getStates() != IPayStates.COMPLETED){
				payInfoService.checkPayInfo(payInfo.getId());
			}
		}
		return "toCheckReceive";
	}
	
	public String queryGoodReceived() throws Exception {
		str ="";
		String mark ="";

		goodReceivedList = goodReceivedService.queryGoodReceived(stuNo, IReceiveStates.COMPLETE);

		if(goodReceivedList.size() != 0 && goodReceivedList != null){
			for(GoodReceivedValue goodReceived : goodReceivedList){
				if(goodReceived.getStates() == IReceiveStates.UNCLAIMED){
					mark="<font color='blue'>未领取</font>";
				}
				if(goodReceived.getStates() == IReceiveStates.NOTCOMPLETE){
					mark="<font color='red'>部分未领取</font>";
				}
				if(goodReceived.getStates() == IReceiveStates.COMPLETE){
					mark="<font color='green'>已全部领取</font>";
				}
				str += "</tr><tr><td>&nbsp;"+goodReceived.getGoodCategory().getCategoryName()+"</td><td colspan='3'>&nbsp;"+this.getGoodNameByCategory(goodReceived.getGoodCategory().getCategoryId())+"</td><td colspan='3'>&nbsp;"+goodReceived.getComments()+"</td><td>&nbsp;"+mark+"</td></tr><tr>";
			}
		}else{
			str += "</tr><tr><td colspan='8'>&nbsp;无记录</td></tr><tr>";
		}
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().write(str);
		response.getWriter().close();
		return null;
	}
	
	public String querInfoForReceived() throws Exception{
		str ="";
		String mark ="";
		int index = 0;

		goodReceivedList = goodReceivedService.queryGoodReceived(stuNo, IReceiveStates.NOTCOMPLETE);

		if(goodReceivedList.size() != 0 && goodReceivedList != null){
			for(GoodReceivedValue goodReceived : goodReceivedList){
				if(goodReceived.getStates() == IReceiveStates.UNCLAIMED){
					mark="<font color='blue'>未领取</font>";
				}
				if(goodReceived.getStates() == IReceiveStates.NOTCOMPLETE){
					mark="<font color='red'>部分未领取</font>";
				}
				if(goodReceived.getStates() == IReceiveStates.COMPLETE){
					mark="<font color='green'>已全部领取</font>";
				}
				str += "</tr><tr><td>&nbsp;"+(++index)+"</td><td>&nbsp;"+goodReceived.getStuInfo().getStuNo()+"</td><td>&nbsp;"+goodReceived.getStuInfo().getStuName()+"</td><td>&nbsp;"+goodReceived.getGoodCategory().getCategoryName()+"</td><td>&nbsp;"+this.getGoodNameByCategory(goodReceived.getGoodCategory().getCategoryId())+"</td><td>&nbsp;"+goodReceived.getComments()+"</td><td>&nbsp;"+mark+"</td><td><a href='javascript:void(0);' onclick='toReceive("+goodReceived.getId()+","+goodReceived.getStuInfo().getStuNo()+");'><img src='themes/icons/edit.png'/></a></td></tr><tr>";
			}
		}else{
			str += "</tr><tr><td colspan='8'>&nbsp;无记录</td></tr><tr>";
		}
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().write(str);
		response.getWriter().close();
		return null;
	}
	
	public String queryGoodByReceiveId () throws Exception{
		str ="";
		GoodReceivedValue goodReceivedValue = goodReceivedService.queryById(id);
		categoryId = goodReceivedValue.getGoodCategory().getCategoryId();
		GoodForCategoryValue goodForCategoryValue = goodForCategoryService.queryByCategoryId(categoryId);
		String goodIds = goodForCategoryValue.getGoodIds();
		String[] goodIdArr = goodIds.split(",");
		String goodName ="";
		str += "</tr><tr><td>&nbsp;"+goodCategoryService.quaryByCategoryId(goodForCategoryValue.getCategoryId()).getCategoryName()+"</td><td colspan='7'>&nbsp;";
		for(String goodId: goodIdArr){
			goodName = goodInfoService.queryById(Integer.parseInt(goodId)).getGoodName();
			str+="<input type='checkbox' id='cb_goodInfo_"+goodId+"' class='cb_goodInfo' name='goodIds' value='"+goodId+"' />"+goodName+"&emsp;";
		}
		str += "<td></tr><tr>||"+goodReceivedValue.getReceived();
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().write(str);
		response.getWriter().close();
		return null;
	}
	
	protected String getGoodNameByCategory(Integer categoryId) throws Exception{
		GoodInfoValue goodInfo = null;
		goodForCategoryValue = goodForCategoryService.queryByCategoryId(categoryId);
		String goodIds = goodForCategoryValue.getGoodIds();
		String[] goodIdArr = goodIds.split(",");
		String goodInfoStr = "";
		for(String goodId : goodIdArr){
			goodInfo = goodInfoService.queryById(Integer.parseInt(goodId));
			goodInfoStr += goodInfo.getGoodName()+", ";
		}
		return goodInfoStr.substring(0, goodInfoStr.length()-2);
	}
	
	public String receiveGood() throws Exception {
		GoodReceivedValue goodReceivedValue = goodReceivedService.queryById(id);
		String newAddGood = this.compareGoodId(goodReceivedValue.getReceived(), checkGoodInfoIds);
		try{
			String received = checkGoodInfoIds;
			
			if(!"".equals(unCheckGoodInfoIds)){
				comments = "未领取："+this.getGoodNameByIds(unCheckGoodInfoIds);
				studentInfoService.setStudentStates(stuId, IStudentStates.UNCLAIMED);
				goodReceivedService.setPayStates(id, IReceiveStates.NOTCOMPLETE);
				str = "1";
			}else{
				str = "2";
				comments = "全部领取";
				goodReceivedService.setPayStates(id, IReceiveStates.COMPLETE);
				goodReceivedList = goodReceivedService.queryGoodReceived(stuNo, IReceiveStates.NOTCOMPLETE);
				if(goodReceivedList.size() == 0 || goodReceivedList == null)
					studentInfoService.setStudentStates(stuId, IStudentStates.COMPLETE);
				
			}
			goodReceivedService.modifyGoodReceived(id, received, comments);
		}catch(Exception e){
			str ="-1";
		}
		
		String[] newAddGoodArr = newAddGood.split(",");
		for(String newAddGoodItem : newAddGoodArr){
			goodInfoService.toSubNum(Integer.parseInt(newAddGoodItem));
		}
		
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().write(str);
		response.getWriter().close();
		
		return null;
	}
	
	protected String compareGoodId(String received, String commit){
		String newAddStr="";
		if(received == null || "".equals(received))
			newAddStr = commit;
		else{
			String[] commitArr=commit.split(",");
			String[] receivedArr = received.split(",");
			for(String commitItem :commitArr){
				boolean mark = false;
				for(String receivedItem : receivedArr){
					if(receivedItem.equals(commitItem)){
						mark = true;
						break;
					}
				}
				if(!mark)
					newAddStr += commitItem+",";
			}
			newAddStr = newAddStr.substring(0, newAddStr.length()-1);
		}
		
		return newAddStr;
	}
	
	protected String getGoodNameByIds(String goodIds) throws Exception{
		String goodNames = "";
		String[] goodIdArr = goodIds.split(",");
		for (String goodId : goodIdArr){
			goodNames += goodInfoService.queryById(Integer.parseInt(goodId)).getGoodName()+",";
		}
		return goodNames.substring(0, goodNames.length() - 1);
	}
	
	// Getter and Setter

	public List<GoodReceivedValue> getGoodReceivedList() {
		return goodReceivedList;
	}

	public void setGoodReceivedList(List<GoodReceivedValue> goodReceivedList) {
		this.goodReceivedList = goodReceivedList;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getStuNo() {
		return stuNo;
	}

	public void setStuNo(String stuNo) {
		this.stuNo = stuNo;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public Integer getStates() {
		return states;
	}

	public void setStates(Integer states) {
		this.states = states;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public String getCheckGoodInfoIds() {
		return checkGoodInfoIds;
	}

	public void setCheckGoodInfoIds(String checkGoodInfoIds) {
		this.checkGoodInfoIds = checkGoodInfoIds;
	}

	public String getUnCheckGoodInfoIds() {
		return unCheckGoodInfoIds;
	}

	public void setUnCheckGoodInfoIds(String unCheckGoodInfoIds) {
		this.unCheckGoodInfoIds = unCheckGoodInfoIds;
	}

	public Integer getStuId() {
		return stuId;
	}

	public void setStuId(Integer stuId) {
		this.stuId = stuId;
	}

	public String getCardId() {
		return cardId;
	}

	public void setCardId(String cardId) {
		this.cardId = cardId;
	}
	
	
}
